window.app = {
	
	/**
	 * netty 聊天
	 */
	nettyServerUrl: "ws://192.168.1.105:8065/ws",
	
	/**
	 * 服务端的地址
	 */
	serverUrl: "http://192.168.1.105:8088",
	
	/**
	 * 判空校验
	 * @param {Object} str
	 */
	isNotNull: function(str) {
		if(str != null && str != "" && str != undefined) {
			return true;
		}
		return false;
	},
	
	/**
	 * plus的toast提示
	 * @param {Object} msg
	 * @param {Object} type
	 */
	showToast: function(msg, type) {
		plus.nativeUI.toast(msg,
			{
				icon: "/img/" + type + ".png", 
				iconWidth: "30px",
				iconHeight: "30px",
				verticalAlign: "center"
			});
	},
	
	/**
	 * 存储用户信息
	 * @param {Object} user
	 */
	setUserInfo: function(user) {
		var userJson = JSON.stringify(user);
		plus.storage.setItem("user", userJson);
	},
	
	/**
	 * 获取用户信息
	 */
	getUserInfo: function() {
		var userJson = plus.storage.getItem("user");
		if(userJson != null) {
			return JSON.parse(userJson);
		}
		return null;
	},
	
	removeUserInfo: function() {
		plus.storage.removeItem("user");
	},
	
	setToken: function(token) {
		plus.storage.setItem("token", token);
	},
	
	getToken: function() {
		return plus.storage.getItem("token");
	},
	
	removeToken: function() {
		plus.storage.removeItem("token");
	},
	
	//存储好友列表
	setFriendList: function(friendList){
		var friendListStr = JSON.stringify(friendList);
		plus.storage.setItem("friendList", friendListStr);
	},
	
	getFriendList: function(){
		var friendListStr = plus.storage.getItem("friendList");
		if(!this.isNotNull(friendListStr)) {
			return [];
		}
		
		return JSON.parse(friendListStr);
	},
	
	getFriendFromFriendList: function(friendId){
		var friendListStr = plus.storage.getItem("friendList");
		if(this.isNotNull(friendListStr)) {
			var friendList = JSON.parse(friendListStr);
			for(var i = 0; i < friendList.length; i++) {
				var friend = friendList[i];
				if(friend.userId == friendId){
					return friend;
					break;
				}
			}
		}else{
			return null;
		}
	},
	
	//存储好友聊天消息
	saveFriendChatHistory: function(myId, friendId, msg, flag){
		var me = this;
		var chatKey = "chat-" + myId + "-" + friendId;
		
		var chatHistoryList;
		var chatHistoryListStr = plus.storage.getItem(chatKey);
		if(me.isNotNull(chatHistoryListStr)){
			chatHistoryList = JSON.parse(chatHistoryListStr);
		}else{
			chatHistoryList = [];
		}
		
		var msgHistory = new me.ChatHistory(myId, friendId, msg, flag);
		chatHistoryList.push(msgHistory);
		
		plus.storage.setItem(chatKey, JSON.stringify(chatHistoryList));
	},
	
	//获取好友聊天消息
	getFriendChatHistory: function(myId, friendId){
		var me = this;
		var chatKey = "chat-" + myId + "-" + friendId;
		
		var chatHistoryList;
		var chatHistoryListStr = plus.storage.getItem(chatKey);
		if(me.isNotNull(chatHistoryListStr)){
			chatHistoryList = JSON.parse(chatHistoryListStr);
		}else{
			chatHistoryList = [];
		}
		
		return chatHistoryList;
	},
	
	removeFriendChatHistory: function(myId, friendId){
		var chatKey = "chat-" + myId + "-" + friendId;
		plus.storage.removeItem(chatKey);
	},
	
	//存储好友聊天快照
	saveFriendChatSnapshot: function(myId, friendId, msg, isRead){
		var me = this;
		var chatSnapshotKey = "chat-snapshot-" + myId;
		
		var chatSnapshotList;
		var chatSnapshotListStr = plus.storage.getItem(chatSnapshotKey);
		if(me.isNotNull(chatSnapshotListStr)){
			chatSnapshotList = JSON.parse(chatSnapshotListStr);
			
			for(var i = 0; i < chatSnapshotList.length; i++) {
				if(chatSnapshotList[i].friendId == friendId) {
					chatSnapshotList.splice(i, 1);
					break;
				}
			}
			
		}else{
			chatSnapshotList = [];
		}
		
		var msgSnapshot = new me.ChatSnapshot(myId, friendId, msg, isRead);
		chatSnapshotList.unshift(msgSnapshot);
		
		plus.storage.setItem(chatSnapshotKey, JSON.stringify(chatSnapshotList));
	},
	
	//获取好友快照
	getFriendChatSnapshot: function(myId){
		var me = this;
		var chatSnapshotKey = "chat-snapshot-" + myId;
		var chatSnapshotList;
		var chatSnapshotListStr = plus.storage.getItem(chatSnapshotKey);
		if(me.isNotNull(chatSnapshotListStr)){
			chatSnapshotList = JSON.parse(chatSnapshotListStr);
		}else{
			chatSnapshotList = [];
		}
		
		return chatSnapshotList;	
	},
	
	readFriendChatSnapshot: function(myId, friendId){
		var chatSnapshotKey = "chat-snapshot-" + myId;
		var chatSnapshotList;
		var chatSnapshotListStr = plus.storage.getItem(chatSnapshotKey);
		if(this.isNotNull(chatSnapshotListStr)){
			chatSnapshotList = JSON.parse(chatSnapshotListStr);
			for(var i = 0; i < chatSnapshotList.length; i++) {
				var friendChat = chatSnapshotList[i];
				if(friendChat.friendId == friendId) {
					friendChat.isRead = true;
					chatSnapshotList.splice(i, 1, friendChat);//替换原来的快照   
					break;
				}
			}
			//替换原有的快照列表
			plus.storage.setItem(chatSnapshotKey, JSON.stringify(chatSnapshotList));
		}else{
			return;
		}
	},
	
	removeFriendChatSnapshot: function(myId, friendId){
		var me = this;
		var chatSnapshotKey = "chat-snapshot-" + myId;
		
		var chatSnapshotList;
		var chatSnapshotListStr = plus.storage.getItem(chatSnapshotKey);
		if(me.isNotNull(chatSnapshotListStr)){
			chatSnapshotList = JSON.parse(chatSnapshotListStr);
			for(var i = 0; i < chatSnapshotList.length; i++) {
				if(chatSnapshotList[i].friendId == friendId) {
					chatSnapshotList.splice(i, 1);
					break;
				}
			}
		}else{
			return;
		}
		
		plus.storage.setItem(chatSnapshotKey, JSON.stringify(chatSnapshotList));
	},
	
	MSG_CONNECT: 1, //"第一次or重连初始化消息"
    MSG_CHAT: 2, //"聊天消息"
    MSG_SIGNED: 3, //"消息签收"
    MSG_KEEP_ALIVE: 4, //"心跳信息"
    MSG_PULL_FRIEND: 5, //拉取好友
    
    MY_MSG: 1, //代表自己发送的
    FRIEND_MSG: 2, //代表朋友发送的
    
    ChatMsg: function(senderId, receiverId, msg, msgId){
    	this.senderId = senderId;
    	this.receiverId = receiverId;
    	this.msg = msg;
    	this.msgId = msgId;
    },
    
    MessageData: function(action, chatMsg, expand) {
    	this.action = action;
    	this.chatMsg = chatMsg;
    	this.expand = expand;
    },
    
    ChatHistory: function(myId, friendId, msg, flag){
    	this.myId = myId;
    	this.friendId = friendId;
    	this.msg = msg;
    	this.flag = flag;
    },
    
    //用于判断消息是否已读
    ChatSnapshot: function(myId, friendId, msg, isRead){
    	this.myId = myId;
    	this.friendId = friendId;
    	this.msg = msg;
    	this.isRead = isRead;
    }
}
